var _ajax = require('../../utils/ajax'); // 请求函数
Page({

    /**
     * 页面的初始数据
     */
    data: {
        number: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        _ajax.orderCourseDetails(options.orderId, function(data) {
            if(data.code == 0){
                that.setData({
                    orderId: options.orderId,
                    payTotal: data.data.course_price,
                    price: data.data.course_price,
                    wxPayData: data.data,
                });
            }
        }, this);
    },
    // 数量函数
    fnCalculationAdd: function() {
        var that = this;
        var numberPara = that.data.number + 1;
        var tPrice = that.data.price * numberPara;
        that.setData({
            number: numberPara,
            payTotal: tPrice
        });
    },
    fnCalculationReduce: function() {
        var that = this;
        var numberPara = that.data.number - 1;
        var tPrice = that.data.price * numberPara;
        if (numberPara <= 0) {
            return;
        } else {
            that.setData({
                number: numberPara,
                payTotal: tPrice
            });
        }
    },
    // 去支付
    fnWxPay: function() {
        var that = this;
        var orderId = that.data.orderId;
        var num = that.data.number;
        _ajax.getWxPay(orderId, num, function(data) {
            var data = data.data;
            wx.requestPayment({
                timeStamp: data.timeStamp,
                nonceStr: data.nonceStr,
                package: data.package,
                signType: 'MD5',
                paySign: data.paySign,
                success(res) { // 支付成功回调
                    wx.redirectTo({
                        url: '../myCourse/myCourse',
                    });
                },
                fail(res) {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'none',
                    });
                }
            })
        }, this);
    }
})